package com.jsyh.shopping.uilibrary;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * 商品条件筛选TabView
 */
public class GoodsFilterTabView extends FrameLayout implements View.OnClickListener {

    private View v;
    //最新
    private LinearLayout newLayout;
    private ImageView newIndicator;
    private TextView newTab;
    //销量
    private TextView saleTab;
    private ImageView saleIndicator;
    //价格
    private LinearLayout priceLayout;
    private TextView priceTab;
    private ImageView priceIndicator;
    private final int PRICE_NORMAL = 0;
    private final int PRICE_UP = 1;
    private final int PRICE_DOWN = 2;
    private int order = PRICE_NORMAL;
    //更新
    private TextView bestLayout;
    private ImageView bestIndicator;
    //筛选
    private LinearLayout filterLayout;
    private TextView filterTab;
    private ImageView filterIndicator;
    private final int NEW = 0;        //最新
    private final int SALE = 1;        //销量
    private final int BEST = 2;        //更新
    private final int PRICE = 3;        //价格
    private int currTab = SALE;
    private static final String DESC = "1";
    private static final String ASC = "2";
    private String orders = DESC;

    public String getOrders() {
        return orders;
    }

    /**
     * 排序的类型
     * 0综合排序
     * 1销量排序
     * 2价格由低到高
     * 3价格又高到低
     * 4人气，
     * （默认为0）
     */
    private final String NEW_DESC = "0";        //new降序
    private final String NEW_ASC = "5";         //new升序
    private final String SALE_DESC = "3";       //销量降序
    private final String SALE_ASC = "6";        //销量升序
    private final String PRICE_ASC = "2";       //价格升序
    private final String PRICE_DESC = "1";      //价格降序
    private final String BEST_DESC = "4";       //更新降序
    private final String BEST_ASC = "7";        //更新降序

    private String currentFilterStr = SALE_DESC;             //当前的筛选条件

    private FilterTabListener tabListener;

    public void setOnTabListener(FilterTabListener tabListener) {
        this.tabListener = tabListener;
    }

    public GoodsFilterTabView(Context context) {
        super(context);
    }

    public GoodsFilterTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }

    public GoodsFilterTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setFilterGone(){
        filterLayout.setVisibility(GONE);
    }

    public void initView(Context context) {
        v = LayoutInflater.from(context).inflate(R.layout.goods_filter_tab_layout, this);

        //最新
        newLayout = (LinearLayout) v.findViewById(R.id.llTabOne);
        newLayout.setOnClickListener(this);
        newTab = (TextView) v.findViewById(R.id.tvTabOneTitle);
        newIndicator = (ImageView) v.findViewById(R.id.ivTabIndication);

        //更新
        bestLayout = (TextView) v.findViewById(R.id.tvTabFive);
        bestIndicator = (ImageView) v.findViewById(R.id.bestIndication);
        bestLayout.setOnClickListener(this);

        //销量
        saleTab = (TextView) v.findViewById(R.id.tvTabTwo);
        saleTab.setOnClickListener(this);
        saleIndicator = (ImageView) v.findViewById(R.id.hotIndication);

        //价格
        priceLayout = (LinearLayout) v.findViewById(R.id.llTabThree);
        priceLayout.setOnClickListener(this);
        priceTab = (TextView) v.findViewById(R.id.tvPriceTitle);
        priceIndicator = (ImageView) v.findViewById(R.id.ivPriceIndicator);

        //筛选
        filterLayout = (LinearLayout) v.findViewById(R.id.llTabFour);
        filterLayout.setOnClickListener(this);
        filterTab = (TextView) v.findViewById(R.id.tvFilterView);
        filterIndicator = (ImageView) v.findViewById(R.id.ivFilterIndicator);

        setTabSaleChecked();

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.llTabOne) {           //综合
            if (currTab != NEW) {
                currTab = NEW;
                setTextViewCheck(newTab, true);
                resetTabHot();
                resetTabPrice();
                resetTabSale();
            }
            switch (order) {
                case PRICE_NORMAL:
                case PRICE_DOWN:
                    order = PRICE_UP;
                    orders = ASC;
                    currentFilterStr = NEW_ASC;
                    newIndicator.setImageResource(R.mipmap.ic_price_order_up);
                    break;
                case PRICE_UP:
                    orders = DESC;
                    order = PRICE_DOWN;
                    currentFilterStr = NEW_DESC;
                    newIndicator.setImageResource(R.mipmap.ic_price_order_down);
                    break;
            }
            if (tabListener != null) {
                tabListener.onFilterCondition(currentFilterStr);
            }

        } else if (i == R.id.tvTabTwo) {     //销量
            if (currTab != SALE) {
                //判断一下，不要重复加载
                currTab = SALE;
                setTextViewCheck(saleTab, true);
                resetTabNew();
                resetTabPrice();
                resetTabHot();
            }
            switch (order) {
                case PRICE_NORMAL:
                case PRICE_DOWN:
                    order = PRICE_UP;
                    orders = ASC;
                    currentFilterStr = SALE_ASC;
                    saleIndicator.setImageResource(R.mipmap.ic_price_order_up);
                    break;
                case PRICE_UP:
                    orders = DESC;
                    order = PRICE_DOWN;
                    currentFilterStr = SALE_DESC;
                    saleIndicator.setImageResource(R.mipmap.ic_price_order_down);
                    break;
            }
            if (tabListener != null) {
                tabListener.onFilterCondition(currentFilterStr);
            }

        } else if (i == R.id.llTabThree) {   //价格
            tabPriceClick();
        } else if (i == R.id.llTabFour) {        //筛选
            if (tabListener != null) {
                tabListener.onFilterListener();
            }
        } else if (i == R.id.tvTabFive) {
            if (currTab != BEST) {
                currTab = BEST;
                resetTabNew();
                resetTabPrice();
                resetTabSale();
                setTextViewCheck(bestLayout, true);
            }
            switch (order) {
                case PRICE_NORMAL:
                case PRICE_DOWN:
                    order = PRICE_UP;
                    orders = ASC;
                    currentFilterStr = BEST_ASC;
                    bestIndicator.setImageResource(R.mipmap.ic_price_order_up);
                    break;
                case PRICE_UP:
                    orders = DESC;
                    order = PRICE_DOWN;
                    currentFilterStr = BEST_DESC;
                    bestIndicator.setImageResource(R.mipmap.ic_price_order_down);
                    break;
            }
            if (tabListener != null) {
                tabListener.onFilterCondition(currentFilterStr);
            }
        }
    }

    private void tabPriceClick() {
        if (currTab != PRICE) {
            currTab = PRICE;
            setTextViewCheck(priceTab, true);
            //还原其他tab
            resetTabNew();
            resetTabSale();
            resetTabHot();
        }

        switch (order) {
            // up
            case PRICE_NORMAL:
            case PRICE_DOWN:
                order = PRICE_UP;
                orders = ASC;
                currentFilterStr = PRICE_ASC;
                priceIndicator.setImageResource(R.mipmap.ic_price_order_up);
                break;
            case PRICE_UP:
                orders = DESC;
                order = PRICE_DOWN;
                currentFilterStr = PRICE_DESC;
                priceIndicator.setImageResource(R.mipmap.ic_price_order_down);
                break;
        }
        if (tabListener != null) {
            tabListener.onFilterCondition(currentFilterStr);
        }
    }

    /**
     * tab one indication roration
     *
     * @param v
     * @param rotation
     */
    private void multipleIndicationRotation(ImageView v, float rotation) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation, v.getWidth() / 2, v.getHeight() / 2);
        v.setImageMatrix(matrix);
    }


    /**
     * 清空TabOne 的选择
     */
    public void resetTabNew() {
        setTextViewCheck(newTab, false);
        newIndicator.setImageResource(R.mipmap.ic_price_normal);
    }


    private void resetTabSale() {
        setTextViewCheck(saleTab, false);
        saleIndicator.setImageResource(R.mipmap.ic_price_normal);
    }

    private void resetTabHot() {
        setTextViewCheck(bestLayout, false);
        bestIndicator.setImageResource(R.mipmap.ic_price_normal);
    }

    private void resetTabPrice() {
        setTextViewCheck(priceTab, false);
        order = PRICE_NORMAL;
        orders = DESC;
        priceIndicator.setImageResource(R.mipmap.ic_price_normal);
    }

    /**
     * 设置销量tab 是选中的  ， 同时回调
     */
    public void setTabSaleChecked() {
        setTextViewCheck(saleTab, true);
        if (tabListener != null) {
            tabListener.onFilterCondition(currentFilterStr);
        }
    }


    /**
     * 设置 筛选的状态 是否选中的
     *
     * @param checked
     */
    public void setFilterChecked(boolean checked) {
        setTextViewCheck(filterTab, checked);
        if (checked) {
            filterIndicator.setImageResource(R.mipmap.ic_filter_tab4_checked);
        } else {
            filterIndicator.setImageResource(R.mipmap.ic_filter_tab4);
        }
    }


    /**
     * 设置文本的 colorSelector
     *
     * @param v
     * @param checked
     */
    public void setTextViewCheck(TextView v, boolean checked) {
        if (checked) {

            v.setTextColor(generatorColorState());
        } else {

            v.setTextColor(getResources().getColor(R.color.text_gary_color));

        }
    }

    /**
     * 生成ColorStateList
     *
     * @return
     */
    public ColorStateList generatorColorState() {
        int[][] states = new int[][]{
                new int[]{android.R.attr.state_pressed},
                new int[]{-android.R.attr.state_pressed},
                new int[]{}
        };

        int[] colors = new int[]{
                getResources().getColor(R.color.text_gary_color),
                getResources().getColor(android.R.color.holo_red_dark),

                getResources().getColor(R.color.text_gary_color)

        };
        return new ColorStateList(states, colors);

    }

    /**
     * 回调监听
     */
    public interface FilterTabListener {
        void onFilterListener();

        void onFilterCondition(String condition);
    }

}
